import 'package:deer/res/colors.dart';
import 'package:deer/res/styles.dart';
import 'package:deer/routers/fluro_navigator.dart';
import 'package:deer/util/utils.dart';
import 'package:deer/widgets/app_bar.dart';
import 'package:deer/widgets/text_field.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:oktoast/oktoast.dart';

class Register extends StatefulWidget {
  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
TextEditingController _nameController = TextEditingController();
  TextEditingController _codeController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  FocusNode _textNode1 = FocusNode();
  FocusNode _textNode2 = FocusNode();
  FocusNode _textNode3 = FocusNode();
  FocusScopeNode _focusScopeNode = FocusScopeNode();
  bool _isClick = false;
  bool _isStartGetCode = false;

  @override
  void initState() {
    super.initState();
    _nameController.addListener(_verify);
    _codeController.addListener(_verify);
    _passwordController.addListener(_verify);
  }

  void _verify() {
    String name = _nameController.text;
    String code = _codeController.text;
    String password = _passwordController.text;
    bool isClick = true;
    if (name.isEmpty || name.length < 11) {
      isClick = false;
    }
    if (code.isEmpty) {
      isClick = false;
    }
    if (password.isEmpty || password.length < 6) {
      isClick = false;
    }

    if (isClick != _isClick) {
      setState(() {
        _isClick = isClick;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: '注册',
      ),
      body: defaultTargetPlatform == TargetPlatform.iOS
          ? FormKeyboardActions(
              child: _buildBody(),
            )
          : SingleChildScrollView(
              child: _buildBody(),
            ),
    );
  }

  _buildBody() {
    return GestureDetector(
      onTap: () {
        if (_focusScopeNode == null) {
          _focusScopeNode = FocusScope.of(context);
        }
        _textNode1.unfocus();
        _textNode2.unfocus();
        _textNode3.unfocus();
      },
      child: Container(
        color: Colors.white,
        child: Padding(
          padding: EdgeInsets.only(left: 16, right: 16, top: 20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text('开启你的账户', style: TextStyles.textBoldDark26,),
              Gaps.vGap(32),
              MyTextField(
                hintText: '请输入账号',
                maxLength: 11,
                focusNode: _textNode1,
                controller: _nameController,
              ),
              Gaps.vGap(10),
              MyTextField(
                hintText: '请输入验证码',
                maxLength: 6,
                focusNode: _textNode2,
                controller: _codeController,
                isStartGetCode: _isStartGetCode,
                getVCode: () {
                  if (_nameController.text.isEmpty) {
                    showToast('请输入手机号码');
                  } else if (_nameController.text.length < 11) {
                    showToast('请输入正确的手机号');
                  } else {
                    setState(() {
                      _isStartGetCode = true;
                    });
                  }
                },
              ),
              Gaps.vGap(10),
              MyTextField(
                hintText: '请输入密码',
                isInputPwd: true,
                focusNode: _textNode3,
                controller: _passwordController,
                maxLength: 16,
                config: Utils.getKeyboardActionsConfig([_textNode1, _textNode2, _textNode3]),
              ),
              Gaps.vGap(25),
              Container(
                height: 44,
                width: double.infinity,
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(3)
                  ),
                  color: Colours.app_main,
                  disabledColor: Colours.login_button_disabled,
                  textColor: Colors.white,
                  disabledTextColor: Colours.login_text_disabled,
                  child: Text('确认', style: TextStyle(fontSize: 18),),
                  onPressed: _isClick ? () {
                    showToast('注册成功');
                    NavigatorUtils.goBack(context);
                  } : null,
                ),
              )
            ],
          ),
        )
      ),
    );
  }
}